package fr.demos.cinepantin.controleur;

import java.io.IOException;

import javax.inject.Inject;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.demos.cinepantin.Article;
import fr.demos.cinepantin.Film;
import fr.demos.cinepantin.Livre;
import fr.demos.cinepantin.dao.DaoArticle;
import fr.demos.cinepantin.dao.DaoFilm;
import fr.demos.cinepantin.dao.DaoLivre;
import fr.demos.cinepantin.dao.interfac.ArticleDao;
import fr.demos.cinepantin.dao.interfac.FilmDao;
import fr.demos.cinepantin.dao.interfac.LivreDao;
import fr.demos.cinepantin.enumerate.Categorie;
import fr.demos.cinepantin.exception.DaoExceptions;

/**
 * @author CQP-DNT
 * Servlet implementation class DetailsArticle
 */
@WebServlet("/DetailsArticle")
public class DetailsArticle extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Inject
	@DaoArticle
	private ArticleDao articledao;
	
	@Inject
	@DaoFilm
	private FilmDao filmdao;
	
	@Inject
	@DaoLivre
	private LivreDao livredao;
	
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DetailsArticle() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher rd;
		request.setAttribute("page", "/detailArticle.jsp");

		/**
		 * Action Afficher les details d'un article en recuperant les images 
		 */

		String action = (String) request.getParameter("action");
		// Integer idArticle = null;
		Article article = null;
		Livre livre = null;
		Film film = null;
		
		try {
			// si l'action details d'un article
//			if (action != null && action.equals("details")) {
				Integer idArticle = Integer.parseInt(request.getParameter("id"));
				// recupere l'article par son id
				Categorie categorie = articledao.getCategorieById(idArticle);
				if(categorie.equals(Categorie.LIVRE)){
					livre=livredao.getLivreById(idArticle);
					
					request.setAttribute("livre", livre);
//					request.setAttribute("edition", livre.getEdition());
				article=livre;
					
				}else if(categorie.equals(Categorie.FILM)){
					film=filmdao.getFilmById(idArticle);
//					request.setAttribute("film", film);
//					request.setAttribute("acteur", film.getActeurs());
//					request.setAttribute("supportFilm", film.getSupportFilm());
//					request.setAttribute("genre", film.getGenre());
//					request.setAttribute("duree", film.getDuree());
					article=film;
					
				}
			   request.setAttribute("article", article);

//				request.setAttribute("idArticle", article.getId());
//				request.setAttribute("titre", article.getTitre());
//				request.setAttribute("resume", article.getResume());
//				request.setAttribute("anciennete", article.getAnciennete());
//				request.setAttribute("anneeDeSortie", article.getAnneeDeSortie());
//				request.setAttribute("tva", article.getTva());
//				request.setAttribute("categorie", article.getCategorie());
//				request.setAttribute("prixVente", article.getPrixVente());
//				request.setAttribute("stock", article.getStock());

//			}
			   
			   
		} catch (DaoExceptions e){
			request.setAttribute("erreurPage",
					"Impossible d'afficher la detail de l'article. ");
		}	
		request.setAttribute("page", "/detailArticle.jsp");
		rd = request.getRequestDispatcher("/page.jsp");
		rd.forward(request, response);
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
